<?php
namespace catchAdmin\product\model;

use catchAdmin\product\model\search\BrandSearch;
use catcher\base\CatchModel;
use think\facade\Db;

class Brand extends CatchModel
{

    use BrandSearch;
    
    protected $name = 'db_brand';
    protected $field = [
            'id', // 
			'brand_id', // 品牌编号
			'brand_name', // 品牌名称
			'createtime', //
			'updatetime', // 创建时间
			'isstatus', // 数据状态
			'sort', // 排序
			'brand_image', // 品牌图片
			'tenant_id', // 商户id
            'creator_id', // 创建者ID
			'created_at', // 创建时间
			'updated_at', // 更新时间
			'deleted_at', // 删除状态，0未删除 >0 已删除
            'manufacturer_id'
    ];
    /**
     * 用户列表
     *
     * @time 2019年12月08日
     * @return array|\think\Paginator
     *@throws \think\db\exception\DbException
     */
    public function getList($tenant_id=0)
    {
        $where = [];
        if($tenant_id !== 0){
            $where[]=['tenant_id','=',$tenant_id];
        }
        return $this->withoutField(['updated_at'], true)
            ->catchSearch()
            ->where($where)
            ->order($this->aliasField('id'), 'desc')
            ->paginate()
            ->each(function (&$item){
                $item['manufacturer_name'] = Db::table('db_manufacturer')
                    ->where([['deleted_at','=',0],['manufacturer_id','=',$item['manufacturer_id']]])
                    ->value('manufacturer_name');
            });
    }
    /**
     * 无分页列表
     *
     * @time 2019年12月08日
     * @return array|\think\Paginator
     *@throws \think\db\exception\DbException
     */
    public function getAttrList($tenant_id=0,$manufacturer_id='')
    {
        $where = [];
        if($tenant_id !== 0){
            $where[]=['tenant_id','=',$tenant_id];
        }
        if($manufacturer_id){
            $where[]=['manufacturer_id','=',$manufacturer_id];
        }
        return $this
            ->catchSearch()
            ->field('id,brand_id,brand_name')
            ->where($where)
            ->order($this->aliasField('id'), 'desc')
            ->select();
    }
}
